package com.quik.web.worker.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.quik.common.Constants;
import com.quik.dto.PaginationDto;
import com.quik.entity.SysAccount;
import com.quik.entity.DtaWorker;
import com.quik.exception.ErrorCode;
import com.quik.web.worker.service.WorkerService;

/**
 * 功能说明：
 *        技师管理的业务控制
 * @author Jenny
 *
 */
@Controller
@RequestMapping("/worker")
public class WorkerController {
	private static Logger log = (Logger)Logger.getLogger(WorkerController.class);
	
	@Autowired
	private WorkerService workerService;
	
	/**
	 * 进入技师管理界面
	 * @return
	 */
	@RequestMapping(value = "workerList", method = RequestMethod.GET)
	public ModelAndView workerList(){
		ModelAndView mav = new ModelAndView("/worker/workerList");
		
		try{
			List workerStatusList = workerService.findWorkerStatus();
			List workerTypeList = workerService.findWorkerType(null, null, null, null);
			
			mav.addObject("gender_man", SysAccount.GENDER_MAN);
			mav.addObject("workerStatusList", workerStatusList);
			mav.addObject("workerTypeList", workerTypeList);
		}catch(Exception e){
			e.printStackTrace();
			log.error("WorkerController workerList error: " + e.getMessage());
		}
		
		return mav;
	}
	
	/**
	 * 根据条件查询技师
	 * @param page
	 * @param rows
	 * @param workerNo
	 * @param workerName
	 * @param workerTypeId
	 * @param workerStatus
	 * @return
	 */
	@RequestMapping(value = "queryWorker", method = RequestMethod.POST)
	@ResponseBody
	public PaginationDto queryWorker(Integer page, Integer rows, String workerNo, String workerName, 
			Integer workerTypeId, String workerStatus){
		PaginationDto dto = new PaginationDto(0, null);
		
		try{
			int intPage = (page == null || page == 0) ? 1:page;  //当前页
		    int pageSize = (rows == null || rows == 0) ? Constants.PAGE_SIZE : rows;  //每页显示的记录
		    
		    int totalCount = workerService.findWorkerCount(workerNo, workerName, workerTypeId, workerStatus);
		    List list = workerService.findWorker(workerNo, workerName, workerTypeId, workerStatus, intPage, pageSize);
		    
		    dto.setRows(list);
		    dto.setTotal(totalCount);
		}catch(Exception e){
			e.printStackTrace();
			log.error("WorkerController queryWorker error: " + e.getMessage());
		}
		
		return dto;
	}
	
	/**
	 * 检查技师编号是否重命
	 * @param workerNo
	 * @return
	 */
	@RequestMapping(value = "checkWorkerNo", method = RequestMethod.POST)
	public ResponseEntity<String> checkWorkerNo(String workerNo){
		boolean flag = false;
		
		try{
			flag = workerService.checkWorkerNo(workerNo);
		}catch(Exception e){
			e.printStackTrace();
			log.error("WorkerController checkWorkerNo error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(flag), HttpStatus.OK);
	}
	
	/**
	 * 进入新增技师界面
	 * @return
	 */
	@RequestMapping(value = "initAddWorker")
	public ModelAndView initAddWorker(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/worker/addWorker");
		
		try{
			List statusList = workerService.findWorkerStatus();
			
			String result = (String)request.getAttribute("result");
			if(result != null && result.length() > 0)
				mav.addObject("result", result);
			
			mav.addObject("gender_man", SysAccount.GENDER_MAN);			
			mav.addObject("gender_women", SysAccount.GENDER_WOMAN);
			mav.addObject("workerStatusList", statusList);
		}catch(Exception e){
			e.printStackTrace();
			log.error("WorkerController initAddWorker error: " + e.getMessage());
		}
		
		return mav;
	}
	
	/**
	 * 新增技师
	 * @param worker
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addWorker", method = RequestMethod.POST)
	public ModelAndView addWorker(DtaWorker worker, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/worker/addWorker");
		String result = ErrorCode.RESULT_ADD_WORKER_FAILED;
		SysAccount account = (SysAccount)request.getSession().getAttribute(Constants.SESSION_ACCOUNT);
		
		try{
			worker.setCrt_date(new java.util.Date(System.currentTimeMillis()));
			worker.setCrt_user(account.getStaff_no());
			
			workerService.addWorker(worker);
			
			result = ErrorCode.RESULT_ADD_WORKER_SUCCESS;			
		}catch(Exception e){
			e.printStackTrace();
			log.error("WorkerController addWorker error: " + e.getMessage());
			result = ErrorCode.SYSTEM_ERROR;
		}
		
		mav.addObject("result", result);
		mav.setViewName("forward:/worker/initAddWorker");
		
		return mav;
	}
}
